from modelscope import snapshot_download
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor, AutoConfig
from qwen_vl_utils import process_vision_info
import torch
from PIL import Image
import requests

# 下载模型
model_dir = snapshot_download('Qwen/Qwen2.5-VL-3B-Instruct')

# 加载配置和模型（禁用量化）
config = AutoConfig.from_pretrained(model_dir)
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    model_dir,
    config=config,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    load_in_4bit=False,
    load_in_8bit=False
)

# 加载处理器
processor = AutoProcessor.from_pretrained(
    model_dir,
    use_fast=True,
    min_pixels=256*28*28,
    max_pixels=1280*28*28
)

# 准备输入数据
image_url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
image_path = "demo.jpg"
Image.open(requests.get(image_url, stream=True).raw).save(image_path)

messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": image_path},
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

# 处理输入
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt"
).to("cuda")

# 生成输出
with torch.no_grad():
    generated_ids = model.generate(**inputs, max_new_tokens=128)
    
output_text = processor.batch_decode(
    generated_ids[:, inputs["input_ids"].shape[1]:],
    skip_special_tokens=True
)
print("Generated Description:", output_text[0])